package com.glory.brepda.adapter;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.GoldenSampleTestDataEntity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;

import java.util.ArrayList;
import java.util.List;

public class GoldenSmapleAdapter extends BaseQuickAdapter<GoldenSampleTestDataEntity.DataEntity, BaseViewHolder> {

    Context context;
    ArrayList<String> codeList = new ArrayList<>();
    ArrayList<String> resultList = new ArrayList<>();
    private FreeUI_GeneralPop mCodePop;
    private FreeUI_GeneralPop resultPop;;
    private FragmentManager supportFragmentManager;

    public GoldenSmapleAdapter(@Nullable List<GoldenSampleTestDataEntity.DataEntity> data, Context context, FragmentManager supportFragmentManager) {
        super(R.layout.item_golden_smaple, data);
        this.context = context;
        this.supportFragmentManager = supportFragmentManager;
        resultList.add("有效");
        resultList.add("无效");
    }

    @Override
    protected void convert(BaseViewHolder helper, GoldenSampleTestDataEntity.DataEntity item) {
        helper.setText(R.id.tvNo,item.getSERIAL_NO());
        helper.setText(R.id.tvTestFLG,item.getTEST_FLG());
        helper.setText(R.id.tvHBIN_NO,item.getHBIN_NO());
        FrameLayout flCode = helper.getView(R.id.flCode);
        ImageView ivCode = helper.getView(R.id.ivCode);
        item.setDataStatus(item.getDataStatus().equals("1")?"1":"0");
        helper.setText(R.id.tvResult,item.getDataStatus().equals("1") ? "有效" : "无效");

        StringBuffer sb = new StringBuffer();
        sb.append(item.getResultInfo().getResultCode() + ',');
        sb.append(item.getResultInfo().getResultInfo() + ';');
        sb.append("\r\n");
        sb.append(item.getResultInfo().getBinNoInfo().getResultCode() + ',');
        sb.append(item.getResultInfo().getBinNoInfo().getResultInfo() + ';');
        sb.append("\r\n");
        sb.append(item.getResultInfo().getBinResultInfo().getResultCode() + ',');
        sb.append(item.getResultInfo().getBinResultInfo().getResultInfo() + ';');
        sb.append("\r\n");
        sb.append(item.getResultInfo().getBinItemInfos().getResultCode() + ',');
        sb.append(item.getResultInfo().getBinItemInfos().getResultInfo() + ';');
        sb.append("\r\n");
        for (GoldenSampleTestDataEntity.BinReturnInfoEntity itemInfo : item.getResultInfo().getBinItemInfos().getItemInfos()) {
            sb.append(itemInfo.getResultCode() + ',');
            sb.append(itemInfo.getResultInfo() + ';');
            sb.append("\r\n");
        }

        TextView tvResultInfo = helper.getView(R.id.tvResultInfo);
        tvResultInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FreeUI_GeneralFragmentDialog tipDialog = FreeApi_DialogUtils.getConfirmDialog(MyApp.app, "确定", sb.toString(), () -> {

                });
                tipDialog.show(supportFragmentManager,"DIALOG");
            }
        });

        flCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCodePop = FreeApi_PopUtils.getNormalPop(context, flCode.getWidth(), codeList, ivCode, (codeS, i) -> {
                    helper.setText(R.id.tvCode,codeS);
                    item.setSampleNo(codeS);
                });
                FreeApi_PopUtils.setArrowShow(ivCode);
                mCodePop.showAsDropDown(flCode, 0, 0);
            }
        });

        FrameLayout flResult = helper.getView(R.id.flResult);
        ImageView ivResult = helper.getView(R.id.ivResult);
        flResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultPop = FreeApi_PopUtils.getNormalPop(context, flResult.getWidth(), resultList, ivResult, (codeS, i) -> {
                    helper.setText(R.id.tvResult,codeS);
                    item.setDataStatus(codeS.equals("有效") ? "1" : "0");
                });
                FreeApi_PopUtils.setArrowShow(ivResult);
                resultPop.showAsDropDown(flResult, 0, 0);
            }
        });
    }

    public void setDataList(ArrayList<String> strings) {
        if(codeList !=null){
            codeList.clear();
        }
        codeList.addAll(strings);
    }
}
